﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerXMLWPF
{
    /// <summary>
    /// Interaction logic for EditMisc.xaml
    /// </summary>
    public partial class EditMisc : Window
    {
        List<string> lstComboboxType;
        List<string> lstComboboxUse;
        public EditMisc()
        {
            InitializeComponent();
            lstComboboxType = new List<string>();
            lstComboboxUse = new List<string>();
            lstComboboxType.Add("Spice");
            lstComboboxType.Add("Fining");
            lstComboboxType.Add("Water Agent");
            lstComboboxType.Add("Herb");
            lstComboboxType.Add("Flavor");
            lstComboboxType.Add("Other");
            lstComboboxUse.Add("Boil");
            lstComboboxUse.Add("Mash");
            lstComboboxUse.Add("Primary");
            lstComboboxUse.Add("Secondary");
            lstComboboxUse.Add("Bottling");
            cbType.ItemsSource = lstComboboxType;
            cbUse.ItemsSource = lstComboboxUse;
            LoadContent("new");
        }

        public EditMisc(string p)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            lstComboboxType = new List<string>();
            lstComboboxType.Add("Spice");
            lstComboboxType.Add("Fining");
            lstComboboxType.Add("Water Agent");
            lstComboboxType.Add("Herb");
            lstComboboxType.Add("Flavor");
            lstComboboxType.Add("Other");
            lstComboboxUse = new List<string>();
            lstComboboxUse.Add("Boil");
            lstComboboxUse.Add("Mash");
            lstComboboxUse.Add("Primary");
            lstComboboxUse.Add("Secondary");
            lstComboboxUse.Add("Bottling");
            cbType.ItemsSource = lstComboboxType;
            cbUse.ItemsSource = lstComboboxUse;
            LoadContent(p);
        }

        private void LoadContent(string s)
        {
            if (s == "new")
            {
                Global.misc = new Misc();
                cbUse.SelectedItem = "Boil";
                cbType.SelectedItem = "Spice";
            }
            else
            {
                txtName.Text = Global.misc.Name;
                cbType.SelectedItem = Global.misc.Type;
                txtAmount.Text = Global.misc.Amount;
                cbUse.SelectedItem = Global.misc.Use;
                txtTime.Text = Global.misc.Time;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter misc name");
                return;
            }
            try
            {
                double amount = double.Parse(txtAmount.Text);
                if (amount <= 0)
                {
                    MessageBox.Show("Amount must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Amount must be a double and greater than 0");
                return;
            }

            try
            {
                double alpha = double.Parse(txtTime.Text);
                if (alpha <= 0)
                {
                    MessageBox.Show("Time must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Time must be a double and greater than 0");
                return;
            }

            Global.misc.Name = txtName.Text;
            Global.misc.Time = double.Parse(txtTime.Text).ToString();
            Global.misc.Amount = double.Parse(txtAmount.Text).ToString();
            Global.misc.Display_Amount = Global.misc.Amount + " kg";
            Global.misc.Use = cbUse.SelectedItem.ToString();
            Global.misc.Type = cbType.SelectedItem.ToString();
            Global.misc.Version = "1";
            Global.OK = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
